#pragma once

#include<iostream>
#include"TcpServer.hpp"
#include"log.hpp"
#include"Protocol.hpp"
#include<signal.h>
#include"Task.hpp"
#include"ThreadPool.hpp"

#define PORT 8081


class HttpServer
{
private:
    int port;
    bool stop;


public:
    HttpServer(int _port = PORT) : port(_port), stop(false) {}

    void InitServer()
    {
        signal(SIGPIPE, SIG_IGN);
       // tcp_server = TcpServer::getinstance(port);
    }                                                        

    void Loop()
    {
        TcpServer* tsvr = TcpServer::getinstance(port);
        LOG(INFO, "LOOP begin");
        while(!stop)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(tsvr->Sock(), (struct sockaddr*)&peer, &len);
            if(sock < 0)
            {
                continue;
            }
            LOG(INFO, "Get a new link...");
            //直接使用线程池
            Task task(sock);
            ThreadPool::getinstance()->PushTask(task);
        }
    }

    ~HttpServer() {} 
};

